package com.yonyou.jrmob.lryapp.view;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.yonyou.jr.mob.rzkh.R;


/**
 * Created by Marvin on 2017/4/14.
 */

public class LoginInput extends FrameLayout {
    public enum INPUT_TYPE {NAME,PASSWORD,TUIGUANGMA}
    private INPUT_TYPE type;
    private View rootView;
    private ImageView icon,clear;
    private EditText content;
    private Context context;
    public LoginInput(@NonNull Context context) {
        super(context);
        this.context = context;
        init();
    }

    public LoginInput(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public LoginInput(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }
    private void init(){
        rootView = LayoutInflater.from(context).inflate(R.layout.layout_login_input,null);
        icon = (ImageView) rootView.findViewById(R.id.login_input_icon);
        clear = (ImageView) rootView.findViewById(R.id.login_input_clear);
        content = (EditText) rootView.findViewById(R.id.login_input_edit);
        clear.setVisibility(INVISIBLE);
        setListener();
        addView(rootView);
    }
    private void setListener(){
        content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (content.getText().length() != 0) {
                    clear.setVisibility(VISIBLE);
                    clear.setClickable(true);
                }else {
                    clear.setVisibility(INVISIBLE);
                    clear.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                content.setText("");
            }
        });
    }

    public EditText getContent() {
        return content;
    }

    public void setType(@NonNull INPUT_TYPE type){
        switch (type) {
            case NAME:
                icon.setImageResource(R.drawable.login_account);
                content.setHint("账号");
                break;
            case PASSWORD:
                icon.setImageResource(R.drawable.login_pw);
                content.setHint("密码");
                content.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                break;
            case TUIGUANGMA:
                icon.setImageResource(R.drawable.login_tuiguang);
                content.setHint("推广码");
                break;
        }
    }

}
